package dp;

/**
 * 152. 乘积最大子数组
 */
public class P152 {
    public int maxProduct(int[] nums) {
        int[][] dp = new int[nums.length][2];
        dp[0][0] = nums[0];
        dp[0][1] = nums[0];

        int max = dp[0][0];
        for (int i = 1; i < nums.length; i++) {
            dp[i][0] = Math.max(nums[i], dp[i - 1][0] * nums[i]);
            dp[i][1] = dp[i - 1][1] * nums[i];
            if (dp[i][0] > max) {
                max = dp[i][0];
            }
            if (dp[i][1] > max) {
                max = dp[i][1];
            }
        }
        return max;
    }

    public static void main(String[] args) {
        int result = new P152().maxProduct(new int[]{2, 3, -2, 4});
        System.out.println(result);
    }
}
